let User = require('../model/user')
let Cash_application = require('../model/cash_application')
let result = require('../utils/result')
let format = require('../utils/format.js')
let Sequelize = require('sequelize');
let sequelize = require('../config/db')
let Op = Sequelize.Op;
let axios = require('axios')
function Cash_applicationController(){

};

/**
 * 申请提款
 * @param req
 * @param res
 * @returns {Promise<void>}
 */

Cash_applicationController.prototype.apply = async (req,res)=>{
    let uid = req.body.uid;
    let _json = {
        account:req.body.account,
        username:req.body.username,
        amount:req.body.amount,
        status_name:'申请中',
        status:1,
        uid:uid
    }
    if(_json.amount<20){
        res.status(200).json(result(-1,'提款金额不能小于20元'))
        return;
    };
    let user = await User.findOne({where:{uid:uid}});
    if(isNaN(_json.amount)){
        res.status(200).json(result(-1,'提款金额只能是数字'))
        return;
    };
    if(_json.amount>user.balance){
        res.status(200).json(result(-1,'提款金额不能大于账户余额'))
        return;
    }
    if(!uid){
        res.json(result(-1,'请求参数不合法'))
        return;
    }
   Cash_application.create(_json).then(()=>{
        let arr = [
            'obV1T5zupOjlONbJz_zCZvYXCLBI',
            'obV1T5yyJEPFNrrrbpq6VtHhRcQw',
            'obV1T531QSVBseNf6nXXuq1sr0AU',
            'obV1T55_02rgaDcPqN-ez5d0_AKY',
            'obV1T53wW4fMxuDKBK44Fci-X9LI']
       let balance = parseFloat(user.balance) - parseFloat(_json.amount);
        for (var i = 0;i<arr.length;i++){
            axios.get('http://localhost:3000/wx/message',{params: {
                    openid: arr[i],
                    model:'u_w_m',
                    nickname:user.nickname,
                    amount:_json.amount
                }})
        }

       User.update({balance:balance},{where:{uid:uid}})
       res.status(200).json(result(0,'申请提款成功'))
   }).catch(()=>{
       res.status(200).json(result(-1,'申请提款失败'))

   })
}


/**
 * 提款申请列表 - 所有
 * @param req
 * @param res
 * @returns {Promise<void>}
 */
Cash_applicationController.prototype.applyList = async (req,res)=>{
    Cash_application.findAll({where:{status:{$lt: 2}},order: sequelize.literal('createdAt DESC')}).then(apply=>{
        res.status(200).json(result(0,'获取成功',apply))
    }).catch(err=>{
        res.status(200).json(result(-1,'获取失败'))

    })
}

/**
 * 提款申请已拒绝
 * @param req
 * @param res
 * @returns {Promise<void>}
 */
Cash_applicationController.prototype.rejected = async (req,res)=>{
    Cash_application.findAll({where:{status: 2},order: sequelize.literal('createdAt DESC')}).then(apply=>{
        res.status(200).json(result(0,'获取成功',apply))
    }).catch(err=>{
        res.status(200).json(result(-1,'获取失败'))

    })
}

/**
 * 提款申请-拒绝
 * @param req
 * @param res
 * @returns {Promise<void>}
 */
Cash_applicationController.prototype.rejecte = async (req,res)=>{
    let id = req.body.id;
    let uid = req.body.uid;
    let _cash = await Cash_application.findOne({where:{id:id}});
    let user =await User.findOne({where:{uid:uid}})
    Cash_application.update({status: 2,status_name:'申请已被拒绝'},{where:{id:id}}).then(()=>{
        let balance = parseFloat(user.balance) + parseFloat(_cash.amount);
        User.update({balance:balance},{where:{uid:uid}});
        Cash_application.findAll({where:{status: 2},order: sequelize.literal('createdAt DESC')}).then(apply=>{
            res.status(200).json(result(0,'获取成功',apply))
        }).catch(err=>{
            res.status(200).json(result(-1,'获取失败'))

        })

    }).catch(err=>{
        res.status(200).json(result(-1,'操作失败'))

    })
}

/**
 * 提款申请已接受
 * @param req
 * @param res
 * @returns {Promise<void>}
 */
Cash_applicationController.prototype.accepted = async (req,res)=>{
    Cash_application.findAll({where:{status: 3},order: sequelize.literal('createdAt DESC')}).then(apply=>{
        res.status(200).json(result(0,'获取成功',apply))
    }).catch(err=>{
        res.status(200).json(result(-1,'获取失败'))

    })
}


/**
 * 提款申-接受
 * @param req
 * @param res
 * @returns {Promise<void>}
 */
Cash_applicationController.prototype.accepte = async (req,res)=>{
    let id = req.body.id;
    Cash_application.update({status: 3,status_name:'申请已通过'},{where:{id:id}}).then(()=>{
        Cash_application.findAll({where:{status: 3},order: sequelize.literal('createdAt DESC')}).then(apply=>{
            res.status(200).json(result(0,'获取成功',apply))
        }).catch(err=>{
            res.status(200).json(result(-1,'获取失败'))

        })
    }).catch(err=>{
        res.status(200).json(result(-1,'操作失败'))

    })
}



let cash_applicationController = new Cash_applicationController();
module.exports = cash_applicationController
